<?php
class Default_Model_DbTable_Page extends Zend_Db_Table_Abstract {
	protected $_name = 'page';
	public function addPage($menu_id, $page_name, $logo, $title, $description , $controller , $block , $status) {
		$data = array (
				'MENU_ID' => $menu_id,
				'PAGE_NAME' => $page_name,
				'LOGO' => $logo,
				'TITLE' => $title,
				'DESCRIPTION' => $description,
				"CONTROLLER" => $controller,
				"BLOCK" => $block,
				'STATUS' => $status 
		);
		return $this->insert ( $data );
	}
	public function updatePage($id, $menu_id, $page_name, $logo, $title, $description , $controller , $block , $status) {
		$data = array (
				'MENU_ID' => $menu_id,
				'PAGE_NAME' => $page_name,
				'LOGO' => $logo,
				'TITLE' => $title,
				'DESCRIPTION' => $description,
				"CONTROLLER" => $controller,
				"BLOCK" => $block,
				'STATUS' => $status  
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function removePage($id) {
		return $this->delete ( "ID = $id" );
	}
	public function activePage($id) {
		$lv = $this->fetchRow ( "ID = $id" );
		if ($lv ["STATUS"] == 1) {
			$this->update ( array (
					"STATUS" => 0 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => 1 
			), "ID = $id" );
		}
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function getPageById($id) {
		return $this->fetchRow ( "ID = $id" )->toArray ();
	}
	public function getPageByName($page_name) {
		return $this->fetchRow ( "PAGE_NAME=$page_name" )->toArray ();
	}
	public function getPageByMenuId($menu_id) {
		return $this->fetchRow ( "MENU_ID = $menu_id" )->toArray ();
	}
	public function getPageByMenuName($menu_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "page AS p", array (
				"p.ID",
				"p.PAGE_NAME",
				"p.LOGO",
				"p.TITLE",
				"p.DESCRIPTION",
				"p.STATUS" 
		) )->where ( "p.STATUS = ?", 1 )->join ( "menu AS m", "m.ID = p.MENU_ID", array (
				"m.MENU_NAME" 
		) )->where ( "m.MENU_NAME = ?", $menu_name );
		try {
			$rows = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		
		return $rows->toArray ();
	}
	public function getAllPage() {
		return $this->fetchAll ()->toArray ();
	}
	
	public function getAllPageHaveParent($parent_id) {
		return $this->fetchAll ( "MENU_ID = $parent_id AND STATUS = 1" )->toArray ();
	}
}

